package com.czj.controller;

import com.czj.common.api.CommonResult;
import com.czj.model.User;
import com.czj.model.Vip;
import com.czj.service.UserService;
import com.czj.service.VipService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.*;


/**
 * 会员管理
 */
@RestController
@Api(tags = "VipController",description = "会员管理")
@RequestMapping("/vip")
public class VipController {
    @Autowired
    private VipService vipService;

    @Autowired
    private UserService userService;
    /**
     * 开通vip
     */
    @ApiOperation(value = "开通vip")
    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public CommonResult<String> registerVip(Principal principal,int time) {
        if (principal == null) {
            return CommonResult.unauthorized(null);
        }
        boolean result = false;
        // 通过 token 中的username 查询该用户的信息（phone，id）
        String userName = principal.getName();
        User user = userService.getUserByUsername(userName);

        //调用支付，判断是否支付成功 TODO
        //先查询是否已开通vip
        Vip vipResult = vipService.select(user.getId());
        // 将查询到的信息放到vip
        Vip vip = new Vip();
        vip.setUserId(user.getId());
        vip.setUserName(userName);
        vip.setCreateTime(new Date()); // 设置创建时间是当前时间
        if (vipResult != null) {
            Calendar calendar = new GregorianCalendar();
            //this.Date.before(give.date)  判断this.date是否早于give.date
            if (vipResult.getEndTime().before(new Date())) { //已开通过vip,未到期
                // 获取之前的vip 结束时间
                calendar.setTime(vipResult.getEndTime());
                calendar.add(Calendar.MONTH,time);
                vip.setEndTime(calendar.getTime());
                // 设置状态 status   0 未开通 ；1 未到期；2  已到期
                vip.setStatus(1);
                System.out.println("vip = " + vip);
                // vip 结束时间增加
                result = vipService.updateEndTime(vip);
            }else { // 之前开通过，已到期
                //设置结束时间和创建时间
                calendar.setTime(vip.getCreateTime());
                calendar.add(Calendar.MONTH,time);
                vip.setEndTime(calendar.getTime());
                vip.setStatus(2);
                result = vipService.updateEndTime(vip);
            }
        } else {  //之前没有开通过vip
            //设置结束时间
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(vip.getCreateTime());
            calendar.add(Calendar.MONTH,time);
            vip.setEndTime(calendar.getTime());
            vip.setStatus(0);
            // 注册vip
            result = vipService.resigter(vip);
        }

        if (!result) {
           return CommonResult.failed();
        }

        return CommonResult.success("开通(续费)成功");
    }
}
